home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Libraries / JPNL Libraries / URL.java < prev   
Text File  |  1996-05-23  |  3KB  |  143 lines

  1. package au.com.peter.libraries;
  2.  
  3. import java.net.MalformedURLException;
  4.  
  5. public class URL {
  6.  
  7.     public String protocol = null;
  8.     public String username = null;
  9.     public String password = null;
  10.     public String host = null;
  11.     public String path = null;
  12.     public int port = 0;
  13.  
  14.     public static URL MakeURL( String url ) {
  15.         URL result;
  16.         
  17.         try {
  18.             result = new URL( url );
  19.         } catch ( MalformedURLException e ) {
  20.             result = null;
  21.         }
  22.         
  23.         return result;
  24.     }
  25.     
  26.     public URL( String url ) throws MalformedURLException {
  27.         try {
  28.             int start, finish;
  29.             
  30.             start = 0;
  31.             finish = url.length() - 1;
  32.             while ( url.charAt( start ) == ' ' ) {
  33.                 start++;
  34.             }
  35.             while ( url.charAt( finish ) == ' ' ) {
  36.                 finish--;
  37.             }
  38.             if ( url.charAt( start ) == '<' ) {
  39.                 start++;
  40.                 if ( url.charAt( finish ) == '>' ) {
  41.                     finish--;
  42.                 } else {
  43.                     throw new MalformedURLException( "missing > in " + url );
  44.                 }
  45.             }
  46.             if ( url.regionMatches( true, start, "url:", 0, 4 ) ) {
  47.                 start += 4;
  48.             }
  49.             int saved_start = start;
  50.             start = url.indexOf( "://", start );
  51.             if ( start < 0 ) {
  52.                 throw new MalformedURLException( "missing :// in " + url );
  53.             }
  54.             protocol = url.substring( saved_start, start - saved_start ).toLowerCase();
  55.             start += 3; // skip ://
  56.             saved_start = start;
  57.             start = url.indexOf( '/', start );
  58.             if ( start < saved_start ) {
  59.                 start = -1;
  60.             }
  61.             path = url.substring( start+1, finish+1 );
  62.             host = url.substring( saved_start, start );
  63.             start = host.lastIndexOf( '@' );
  64.             if ( start >= 0 ) {
  65.                 username = host.substring( 0, start );
  66.                 host = host.substring( start + 1 );
  67.                 start = username.lastIndexOf( ':' );
  68.                 if ( start >= 0 ) {
  69.                     password = username.substring( start + 1 );
  70.                     username = username.substring( 0, start );
  71.                 }
  72.             }
  73.             start = host.lastIndexOf( ':' );
  74.             if ( start >= 0 ) {
  75.                 port = Integer.parseInt( host.substring( start + 1 ) );
  76.                 host = host.substring( start + 1 );
  77.             } else {
  78.                 port = ProtocolDefaultPort( protocol );
  79.             }
  80.         } catch ( MalformedURLException e ) {
  81.             throw e;
  82.         } catch ( Exception e ) {
  83.             throw new MalformedURLException( "malformed: " + url );
  84.         }
  85.     }
  86.  
  87.     public void SetDefaultUsername( String email ) {
  88.         if ( username == null ) {
  89.             username = "anonymous";
  90.         }
  91.         if ( password == null ) {
  92.             password = email;
  93.         }
  94.     }
  95.     
  96.     public static int ProtocolDefaultPort( String prot ) { // improve later
  97.         if ( prot.equals( "ftp" ) ) {
  98.             return 21;
  99.         } else if ( prot.equals( "http" ) ) {
  100.             return 80;
  101.         } else if ( prot.equals( "news" ) || prot.equals( "nntp" ) ) {
  102.             return 119;
  103.         } else if ( prot.equals( "smtp" ) ) {
  104.             return 25;
  105.         }
  106.         return 0;
  107.     }
  108.     
  109.     public boolean IsAnonymous() {
  110.         if ( username == null || username.length() == 0 ) {
  111.             return true;
  112.         }
  113.         return username.equalsIgnoreCase( "ftp" ) || username.equalsIgnoreCase( "anonymous" );
  114.     }
  115.     
  116.     public String toString( boolean secret ) {
  117.         String result = protocol + "://";
  118.         if ( !IsAnonymous() ) {
  119.             result += username;
  120.             if ( password != null ) {
  121.                 result += ":";
  122.                 if ( !secret ) {
  123.                     result += password;
  124.                 }
  125.             }
  126.             result += "@";
  127.         }
  128.         result += host;
  129.         if ( port == ProtocolDefaultPort( protocol ) ) {
  130.             result += ":" + port;
  131.         }
  132.         if ( path != null ) {
  133.             result += "/" + path;
  134.         }
  135.         return result;
  136.     }
  137.     
  138.     public String toString() {
  139.         return toString( true );
  140.     }
  141.     
  142.     
  143. }